import 'package:flutter/material.dart';
import 'package:ref/ref.dart';

import 'state.dart';

class TestCompute extends StatelessWidget {
  const TestCompute({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Text('-----测试compute-----'),
        Wrap(
          children: [
            MvvmDemo01(),
            MvvmDemo02(),
          ],
        )
      ],
    );
  }
}

// -------------------------------------------------------------------------------------------------------
class MvvmDemo01 extends StatefulWidget {
  const MvvmDemo01({Key? key}) : super(key: key);
  @override
  _MvvmDemo01State createState() => _MvvmDemo01State();
}

class _MvvmDemo01State extends State<MvvmDemo01> {
  add() {
    fatherInfo.set(() {
      fatherInfo.value['info']['age'] += 5;
    });
  }

  minus() {
    fatherInfo.value['info']['age'] -= 5;
    fatherInfo.value = fatherInfo;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width / 2,
      color: Colors.black.withOpacity(0.1),
      padding: EdgeInsets.all(5),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          RefBuilder(
            () => Text(
              "爸爸 " +
                  fatherInfo.value['info']['name'] +
                  "今年" +
                  fatherInfo.value['info']['age'].toString(),
              style: TextStyle(fontSize: 20),
            ),
          ),
          TextButton(onPressed: add, child: Text('爸爸老了')),
          TextButton(onPressed: minus, child: Text('爸爸变年轻了')),
        ],
      ),
    );
  }
}

// ---------------------------------------------------------------------------------------------------
class MvvmDemo02 extends StatefulWidget {
  const MvvmDemo02({Key? key}) : super(key: key);
  @override
  _MvvmDemo02State createState() => _MvvmDemo02State();
}

class _MvvmDemo02State extends State<MvvmDemo02> {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width / 2,
      color: Colors.black.withOpacity(0.2),
      padding: EdgeInsets.all(5),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            "爸爸比儿子大了20岁",
            style: TextStyle(fontSize: 16),
          ),
          Text(
            "儿子今年多少岁？",
            style: TextStyle(fontSize: 16),
          ),
          RefBuilder(
            () => Text(
              childAge.value.toString(),
              style: TextStyle(fontSize: 24, color: Colors.red),
            ),
          ),
        ],
      ),
    );
  }
}
